
#include "stm32f10x.h"
#include "config.h"
#include "w5200.h"
#include "socket.h"
#include "util.h"
#include "loopback.h"
#include <stdio.h>

extern u8 ch_status[MAX_SOCK_NUM];

void loopback_tcps(u8 ch, u16 port)
{
	int len;		
	
	u8 * data_buf = (u8*) TX_BUF;

	switch (getSn_SR(ch))
	{
	case SOCK_ESTABLISHED:						/* if connection is established */
		if(ch_status[ch]==1)
		{
			printf("\r\n%d : Connected",ch);
			ch_status[ch] = 2;
		}
		if ((len = getSn_RX_RSR(ch)) > 0) 			/* check Rx data */
		{
			if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE;	/* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */
									/* the data size to read is MAX_BUF_SIZE. */
			len = recv(ch, data_buf, len);			/* read the received data */
			send(ch, data_buf, len);				/* send the received data */
		}
		break;
	case SOCK_CLOSE_WAIT:                           		/* If the client request to close */
		printf("\r\n%d : CLOSE_WAIT", ch);
		disconnect(ch);
		ch_status[ch] = 0;
		break;
	case SOCK_CLOSED:                                               /* if a socket is closed */
		if(!ch_status[ch]) 
		{
			printf("\r\n%d : Loop-Back TCP Server Started.",ch);
			ch_status[ch] = 1;
		}
		if(socket(ch,Sn_MR_TCP,port,0x00) == 0)    /* reinitialize the socket */
		{
			printf("\r\n%d : Fail to create socket.",ch);
			ch_status[ch] = 0;
		}
		else	listen(ch);
		break;
	}
}

#if 0
void loopback_tcpc(u8 ch, CHConfig_TypeDef* ChConfig)
{
	int len;							
	u8 * data_buf = (u8*) TX_BUF;

	switch (getSn_SR(ch))
	{
	case SOCK_ESTABLISHED:						/* if connection is established */
		if(ch_status[ch]==1)
		{
			printf("\r\n%d : Connected",ch);			
			ch_status[ch] = 2;
		}
		if ((len = getSn_RX_RSR(ch)) > 0) 			/* check Rx data */
		{
			if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE;	/* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */
									/* the data size to read is MAX_BUF_SIZE. */
			len = recv(ch, data_buf, len);			/* read the received data */
			send(ch, data_buf, len);				/* send the received data */
		}
		break;
	case SOCK_CLOSE_WAIT:                           		/* If the client request to close */
		printf("\r\n%d : CLOSE_WAIT", ch);
		disconnect(ch);
		ch_status[ch] = 0;
		break;
	case SOCK_CLOSED:                                               /* if a socket is closed */
		if(!ch_status[ch])
		{
			printf("\r\n%d : Loop-Back TCP Client Started.",ch);
			ch_status[ch] = 1;
		}
		if(socket(ch,Sn_MR_TCP,get_system_any_port(),0x00) == 0)    /* reinitialize the socket */
		{
			printf("\a%d : Fail to create socket.",ch);
			ch_status[ch] = 0;
		}
		else	connect(ch, ChConfig.destip, ChConfig.port);
		break;
	}
}

#endif

void loopback_udp(u8 ch, u16 port)
{
	int len;
	u8 * data_buf = (u8*) TX_BUF;
	u32 destip = 0;
	u16 destport;

	switch (getSn_SR(ch))
	{
	case SOCK_UDP:
		if ((len = getSn_RX_RSR(ch)) > 0) 			/* check Rx data */
		{
			if (len > TX_RX_MAX_BUF_SIZE) len = TX_RX_MAX_BUF_SIZE;	/* if Rx data size is lager than TX_RX_MAX_BUF_SIZE */
									/* the data size to read is MAX_BUF_SIZE. */
			len = recvfrom(ch, data_buf, len,(u8*)&destip,&destport);			/* read the received data */
			if(sendto(ch, data_buf, len,(u8*)&destip,destport) == 0)	/* send the received data */
			{
				printf("\a\a\a%d : System Fatal Error.", ch);
			}
		}
		break;
	case SOCK_CLOSED:                                               /* if a socket is closed */
		printf("\r\n%d : Loop-Back UDP Started.",ch);
		if(socket(ch,Sn_MR_UDP,port,0x00)== 0)    /* reinitialize the socket */
			printf("\a%d : Fail to create socket.",ch);
		break;
	}
}

